/**
 @Name：layuiAdmin 主页控制台
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：GPL-2

 */
layui.define(function (exports) {
    /*
      下面通过 layui.use 分段加载不同的模块，实现不同区域的同时渲染，从而保证视图的快速呈现
    */
    //区块轮播切换
    layui.use(['admin', 'carousel','util'], function () {
        var $ = layui.$
            , admin = layui.admin
            , carousel = layui.carousel
            , element = layui.element
            , device = layui.device()
            , util = layui.util;

        //轮播切换
        $('.layadmin-carousel').each(function () {
            var othis = $(this);
            carousel.render({
                elem: this
                , width: '100%'
                , arrow: 'none'
                , interval: othis.data('interval')
                , autoplay: othis.data('autoplay') === true
                , trigger: (device.ios || device.android) ? 'click' : 'hover'
                , anim: othis.data('anim')
            });
        });

        element.render('progress');

    });

    //数据概览
    layui.use(['carousel', 'echarts'], function () {
        var $ = layui.$
            , carousel = layui.carousel
            , echarts = layui.echarts;

        var echartsApp = [], options = [
            //访客浏览器分布
            {
                title: {
                    text: '访客浏览器分布',
                    x: 'center',
                    textStyle: {
                        fontSize: 14
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    data: type
                },
                series: [{
                    name: '访问来源',
                    type: 'pie',
                    radius: '55%',
                    center: ['50%', '50%'],
                    data: terminal
                }]
            },
            //新增的用户量
            {
                title: {
                    text: '最近一周新增的用户量',
                    x: 'center',
                    textStyle: {
                        fontSize: 14
                    }
                },
                tooltip: { //提示框
                    trigger: 'axis',
                    formatter: "{b}<br>新增用户：{c}"
                },
                xAxis: [{ //X轴
                    type: 'category',
                    data: xAxis
                }],
                yAxis: [{  //Y轴
                    type: 'value'
                }],
                series: [{ //内容
                    type: 'line',
                    data: series,
                }]
            }
        ]
            , elemDataView = $('#LAY-index-dataview').children('div')
            , renderDataView = function (index) {
            echartsApp[index] = echarts.init(elemDataView[index], layui.echartsTheme);
            echartsApp[index].setOption(options[index]);
            window.onresize = echartsApp[index].resize;
        };


        //没找到DOM，终止执行
        if (!elemDataView[0]) return;


        renderDataView(0);

        //监听数据概览轮播
        var carouselIndex = 0;
        carousel.on('change(LAY-index-dataview)', function (obj) {
            renderDataView(carouselIndex = obj.index);
        });

        //监听侧边伸缩
        layui.admin.on('side', function () {
            setTimeout(function () {
                renderDataView(carouselIndex);
            }, 300);
        });

        //监听路由
        layui.admin.on('hash(tab)', function () {
            layui.router().path.join('') || renderDataView(carouselIndex);
        });
    });


    //最新
    layui.use(['table','util','admin'], function () {
        var $ = layui.$, table = layui.table, admin = layui.admin, util = layui.util;
        var addtime = function(d){
            return util.toDateString(d.addtime*1000,'yyyy-MM-dd HH:mm:ss');
        };
        var status = function(d){
            return d.status == 1 ? "<font color='#009688'>已结</font>" : "<font color='#FF5722'>未结</font>";
        };
        var action = function(d){
            return '<a class="layui-btn layui-btn-xs" lay-event="reply">查看</a><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
        };
        //最新留言
        table.render({
            elem: '#message'
            , url: '/admin/Message/index' //模拟接口
            , method: 'post'
            , page: true
            , cols: [[
                {type: 'numbers', fixed: 'left'},
                {field: 'username', title: '用户名'},
                {field: 'title', title: '标题'},
                {field: 'addtime', title: '发布时间', templet: addtime},
                {field: 'status', align: 'center', title: '状态', templet:status },
                {width: 150, align: 'center', toolbar: '#action'}
            ]]
            , skin: 'line'
        });
        //监听工具条
        table.on('tool(message)', function (obj) {
            var data = obj.data;
            var id = data.id;
            if (obj.event === 'del') {
                layer.confirm('您确定要删除该留言吗？', function () {
                    admin.req({
                        url: '/admin/message/del/'
                        , type: 'post'
                        , data: {'id': id}
                        , success: function (res) {
                            layer.closeAll('dialog');
                            if (res.code == 0) {
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    obj.del();
                                });
                            }
                        }
                    });
                });
            } else if (obj.event === 'reply') {
                var index = layer.open({
                    type: 2,
                    content: '/admin/message/reply/id/' + id,
                    area: ['300px', '300px'],
                    maxmin: true
                });
                layer.full(index);
            }
        });
        /*//今日热贴
        table.render({
          elem: '#LAY-index-topCard'
          ,url: layui.setter.base + 'json/console/top-card.js' //模拟接口
          ,page: true
          ,cellMinWidth: 120
          ,cols: [[
            {type: 'numbers', fixed: 'left'}
            ,{field: 'title', title: '标题', minWidth: 300, templet: '<div><a href="{{ d.href }}" target="_blank" class="layui-table-link">{{ d.title }}</div>'}
            ,{field: 'username', title: '发帖者'}
            ,{field: 'channel', title: '类别'}
            ,{field: 'crt', title: '点击率', sort: true}
          ]]
          ,skin: 'line'
        });*/
    });

    exports('console', {})
});